#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys, os.path
import time
import traceback

def now() :
    return time.strftime('%Y-%m-%d %H:%M:%S ')

def error(format, *args) :
    sys.stderr.write(now() + 'ERROR ')
    sys.stderr.write(format % args)
    sys.stderr.write('\n')

def warn(format, *args) :
    sys.stderr.write(now() + 'WARN  ')
    sys.stderr.write(format % args)
    sys.stderr.write('\n')

def info(format, *args) :
    sys.stderr.write(now() + 'INFO  ')
    sys.stderr.write(format % args)
    sys.stderr.write('\n')

def debug(format, *args) :
    filename, lineno, module, function = __tracestack()
    sys.stderr.write(now() + '%s:%d DEBUG ' % (filename, lineno))
    sys.stderr.write(format % args)
    sys.stderr.write('\n')

def exception() :
    sys.stderr.write(now() + 'ERROR ')
    sys.stderr.write(''.join(traceback.format_exception(*sys.exc_info())))
    sys.stderr.write('\n')

def __tracestack() :
    stack = traceback.extract_stack()
    LAYER = -3
    filename = os.path.basename(stack[LAYER][0])
    lineno   = stack[LAYER][1]
    module   = stack[LAYER][2]
    function = stack[LAYER][3]
    return  (filename, lineno, module, function)
